﻿using NxStoreManage;

using System;
using System.IO;

namespace CallNumberSystem
{
    internal class WriteLogHelper
    {
        public static string LogDir = LengthAiCalculate.Properties.Settings.Default.logDir;

        public static void InitDefaultLogDir()
        {
            DriveInfo[] drives = DriveInfo.GetDrives();
            bool driveIsNull = false;
            string firstDrive = string.Empty;
            if (!string.IsNullOrEmpty(LogDir))
            {
                foreach (DriveInfo drive in drives)
                {
                    if (drive.IsReady && drive.Name == LogDir.Substring(0, 3))
                    {
                        driveIsNull = true;
                        break;
                    }
                    if (drive.IsReady && firstDrive == string.Empty)
                    {
                        firstDrive = drive.Name;
                    }
                }
            }
            else
            {
                foreach (DriveInfo drive in drives)
                {
                    if (drive.IsReady && firstDrive == string.Empty)
                    {
                        firstDrive = drive.Name;
                    }
                }
            }
            if (!driveIsNull)
            {
                LogDir = firstDrive + @"LengthAiCalculateLog\";
                LengthAiCalculate.Properties.Settings.Default.logDir = LogDir;
                LengthAiCalculate.Properties.Settings.Default.Save();
            }
        }

        public static void WriteLog(string logStr, LogType logType)
        {
            string strFileName = string.Format("LengthAiCalculateLog{0}.txt", DateTime.Now.ToString("yyyyMMdd"));
            FileStream fs = null;
            StreamWriter sw = null;
            try
            {
                //判断文件夹是否存在
                if (!Directory.Exists(LogDir))
                {
                    Directory.CreateDirectory(LogDir);
                }
                string strPath = LogDir + strFileName;
                logStr = logStr.Replace("\r\n", "");
                string strTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                string msg = logType.ToString() + "=>" + strTime + "：" + logStr;
                if (File.Exists(strPath)) //如果文件存在就追加日志记录
                {
                    fs = new FileStream(strPath, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
                }
                else //如果文件不存在，就创建日志文件
                {
                    fs = new FileStream(strPath, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
                }
                sw = new StreamWriter(fs);
                sw.WriteLine(msg);
                sw.Flush();
                sw.Close();
                fs.Close();
                sw = null;
                fs = null;
            }
            catch
            {
                BaseForm.ShowError("默认日志保存磁盘不存在，已将日志保存到：" + LogDir + "路径");
            }
            finally
            {
                if (sw != null)
                {
                    sw.Flush();
                    sw.Close();
                }
                if (fs != null)
                {
                    fs.Close();
                }
            }
        }
    }

    internal enum LogType
    {
        SocketOK = 1,
        SocketERROR = 0,
        DatabaseERROR = 2
    }
}